﻿#region Header

/*
Remote views based on linq
By

Pascal Hauser 
Dipl. Ing. in Informatik, Hochschule für Technik Rapperswil, 2006
Master Thesis, Hochschule für Technik Rapperswil, 2008-2010

and

Raphael Gfeller
Dipl. Ing. in Informatik, Hochschule für Technik Rapperswil, 2006
Master Thesis, Hochschule für Technik Rapperswil, 2008-2010

*/

#endregion

#region Usings

using System;
using System.Collections.Generic;
using System.Linq;

#endregion

namespace Utils {
	/// <summary>
	/// Description of the class
	/// </summary>
	[Serializable]
	public class PerformanceMeasurements {
		#region Public Methods

		public void AddTaskToMessure<T>(string name, DelegateWithOneArgument<T> method, T args) {
			m_tasks.Add(name, method);
			m_args.Add(args);
		}

		public KeyValuePair<string, double>[] Run() {
			var toReturn = new Dictionary<string, double>();
			var timer = new PerformanceTimer();
			int index = 0;
			foreach (var task in m_tasks) {
				timer.Reset();
				timer.Start();
				task.Value.DynamicInvoke(new[] { m_args[index] });
				timer.Stop();
				toReturn.Add(task.Key, timer.ElapsedTimeMilliseconds.Miliseconds);
				index++;
			}
			return toReturn.ToArray();
		}

		#endregion

		#region Private Fields

		private readonly List<Object> m_args = new List<object>();
		private readonly Dictionary<string, Delegate> m_tasks = new Dictionary<string, Delegate>();

		#endregion
	}
}